package com.xuan.yuapiinterface.controller;


import com.xuan.yuapiclientsdk.model.User;
import com.xuan.yuapiclientsdk.utils.SignUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class InterfaceController {

    @GetMapping("/")
    public String getNameByGet(String name) {
        return "GET 你的名字是: " + name;
    }

    @PostMapping("/")
    public String getNameByPost(String name) {
        return "POST 你的名字是: " + name;
    }

    @PostMapping("/json")
    public String getNameByJson(@RequestBody User user, HttpServletRequest request) {
//        if (user == null) {
//            throw new RuntimeException("参数为空");
//        }
//        String name = user.getName();
//        if (!StringUtils.hasLength(name)) {
//            throw new RuntimeException("参数为空");
//        }
//        // TODO 从登录用户获取 accessKey和secretKey
//        String accessKey = request.getHeader("accessKey");
//        if (!accessKey.equals("xuancc")) {
//            throw new RuntimeException();
//        }
//        String nonce = request.getHeader("nonce");
//        if (Integer.parseInt(nonce) > 10000) {
//            throw new RuntimeException();
//        }
//        // 当前时间大于 timestamp 超过 5分钟则视为无效(过期)
//        final long expireTime = 1000 * 60 * 5;
//        String timestamp = request.getHeader("timestamp");
//        if (System.currentTimeMillis() - Long.parseLong(timestamp) > expireTime) {
//            throw new RuntimeException();
//        }
//        // TODO 使用相同参数(secretKey)和签名算法计算签名
//        String clientSign = request.getHeader("sign");
//        String body = request.getHeader("body");
//        String serverSign = SignUtils.getSign(body, "abcdefg");
//        if (!serverSign.equals(clientSign)) {
//            throw new RuntimeException();
//        }
        String name = user.getName();
        return "POST[JSON] 你的名字是: " + name;
    }
}
